package com.zzk.lanouhn.myapplication1.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageButton;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.AllGoodShowAdapter;
import com.zzk.lanouhn.myapplication1.modles.AllShowGoods;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;

/**
 * 全部晒单页面
 * Created by lanouhn on 16/9/13.
 */
public class AllShowGoodsActivity extends AppCompatActivity{
    private ImageButton btn_back;
    private XRecyclerView recyclerView;
    private String singleId;
    private AllShowGoods goodDetails;
    private AllGoodShowAdapter adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_show_goods);
        initView();
        initData();
        initEvent();
        getData();
    }
    private void initView(){
        btn_back= (ImageButton) findViewById(R.id.back_act_all_shows);
        recyclerView= (XRecyclerView) findViewById(R.id.recycler_all_shows);
    }
    private void initData(){
        Intent intent=getIntent();
        singleId=intent.getStringExtra("singleId");
    }
    private void initEvent(){
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void getData(){
        String url="http://open4.bantangapp.com/products/brandProduct/relationPostList?";
        String dataUrl="app_id=com.jzyd.BanTang&client_id=bt_app_android" +
                "&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7&os_versions=5.0.2&screensize=720&v=19&page=0&pagesize=10" +
                "&id="+singleId;
        try {
            HttpUtils.doPostAsyn(url, dataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson=new Gson();
                    goodDetails=gson.fromJson(result,AllShowGoods.class);
                    if (goodDetails.getData().size()>0) {
                        handler.sendEmptyMessage(1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 1:
                    adapter=new AllGoodShowAdapter(AllShowGoodsActivity.this,goodDetails);
                    recyclerView.setAdapter(adapter);
                    LinearLayoutManager manager=new LinearLayoutManager(AllShowGoodsActivity.this);
                    manager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(manager);
                    break;
              default:

                  break;
            }
        }
    };
}
